<form name="formulario" action="det.php" method="get">
<select name="indice" onchange="formulario.submit()">
<?php
$xml_file = "/home/projetos/anaweb/anaweb-new/bibliotecas/bib_demo.xml";
getMatrices($xml_file);
createMenu();
?>
</select>
</form>
<?
function getMatrices($xml_file) {
    global $matrizes;
    $objeto  = new DOMDocument();
    $objeto->load($xml_file);
    $filho = $objeto->getelementsbytagname("matrices");
    if (!empty($filho)) {
        $temp = $filho->item(0)->getelementsbytagname("matrix");
        $j = 0;
        for($i = 0; $i < $temp->length; $i++) {
            $conteudo = $temp->item($i)->getelementsbytagname("dim");
            if ($conteudo->length > 0) {
            $matrizes[$j] = $temp->item($i);
            $j++;
            }
        }
        return $matrizes;
    }
    else return null;
}

function getDim($indice) {
    global $matrizes, $dim;
    $dimensao = $matrizes[$indice]->getelementsbytagname("dim");
    $dim = $dimensao->item(0)->nodeValue;
    return $dim;
}

function getDesc($indice) {
    global $matrizes, $desc;
    $descricao = $matrizes[$indice]->getelementsbytagname("description");
    $desc = $descricao->item(0)->nodeValue;
    return $desc;
}

function getCom($indice) {
    global $matrizes, $com;
    $comentario = $matrizes[$indice]->getelementsbytagname("comments");
    if ($comentario->length > 0) {
        $com = $comentario->item(0)->nodeValue;
        return $com;
    }
    else return null;
}

function getRef($indice) {
    global $matrizes, $ref;
    $referencia = $matrizes[$indice]->getelementsbytagname("reference");
    if ($referencia->length > 0) {
        $ref = $referencia->item(0)->nodeValue;
        return $ref;
    }
    else return null;
}

function getMatrix($indice) {
    global $matrizes, $A;
    $linhas = $matrizes[$indice]->getelementsbytagname("row");
    for($i = 0; $i < $linhas->length; $i++) {
        $x = $linhas->item($i)->getelementsbytagname("x");
        for($j = 0; $j < $x->length; $j++) {
            $A[$i][$j] = $x->item($j)->nodeValue;
        }
    }
    return $A;
}


function createMenu() {
    global $matrizes;
    for($i = 0; $i < sizeof($matrizes); $i++) {
        echo "<option value=\"$i\"";
        if ($i == @$_GET['indice']) echo " selected";
        echo ">matriz ".($i+1)." (dim ".getDim($i).")</option>\n";
    }
    return;
}
$xml_file = "/home/projetos/anaweb/anaweb-new/bibliotecas/bib_demo.xml";
if (isset($_GET['indice']))
    $indice = $_GET['indice'];
else
    $indice = 0;
getMatrices($xml_file);
$dim = getDim($indice);
$desc = getDesc($indice);
$com = getCom($indice);
$ref = getRef($indice);
$A = getMatrix($indice);
require_once("../classes/cas.php");
echo "<strong>Matriz:</strong>";
echo "<table>";
$dim = count($A);
for ($i = 0 ; $i < $dim; $i++)
{
    echo "<tr>";
    for ($j = 0; $j < count($A[$i]); $j++)
    {
        echo "<td>".$A[$i][$j]."</td>";
    }
    echo "</tr>";
}
echo "</table>";
echo "<p><strong>Determinante pelo Scilab:</strong> ";
$scilab = new CAS();
$scilab->loadMatrix($A, "A");
$scilab->addParam("det(A)");
$scilab->exec();
echo $scilab->getdet()."</p>";
$scilab->disp();

$det = 1;
for ($k=0; $k<$dim-1; $k++)
{
    for ($i=$k+1; $i<$dim; $i++)
    {
        if ($A[$k][$k]==0)
        {   // pivo se anula
            $maior = abs($A[$k][$k]);
            for ($cont=$k+1; $cont<$dim; $cont++)
            {
                if (abs($A[$cont][$k]) > $maior) {
                    $maior = abs($A[$cont][$k]);
                    $indice = $cont;
                }
            }
            $troca = $indice + 1;
            for ($l=0; $l<=$dim; $l++) { //troca de linha
                $aux2=$A[$k][$l];
                $A[$k][$l]=$A[$indice][$l];
                $A[$indice][$l]=$aux2;
            }
            $det = -$det;
        } // end if
        $M[$i][$k]=-$A[$i][$k]/$A[$k][$k];
        for ($j=0; $j<$dim; $j++) {
            $A[$i][$j]+=$M[$i][$k]*$A[$k][$j];
        }
    }
}
for ($i=0; $i<$dim; $i++) {
        $det = $det * $A[$i][$i];
}

echo "<p><strong>Determinante pelo ANAWEB:</strong> $det</p>";
?>
